package view;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;

import common.Utils;

import model.DrawObject;

public class ViewLine extends ViewObject
{

	public ViewLine(DrawObject obj, Graphics g) 
	{
		super(obj, g);
	}

	public ViewLine(DrawObject obj, Graphics2D g2) 
	{
		super(obj, g2);
	}

	@Override
	public void Draw(Graphics g)
	{
		super.Draw(g);
		
		Graphics2D g2 = (Graphics2D)g;
		
		Color c = new Color(dObj.getRed(), dObj.getGreen(), dObj.getBlue());
		g2.setColor(c);
		
		g2.setStroke(new BasicStroke( (float)dObj.getThickness() ));
		
		g2.drawLine(dObj.getX1(), dObj.getY1(), dObj.getX2(), dObj.getY2());
	}

	@Override
	public int pFuncX(double t)
	{
		return (int)( dObj.getX1() + t*(dObj.getX2() - dObj.getX1()) );
	}

	@Override
	public int pFuncY(double t)
	{
		return (int)( dObj.getY1() + t*(dObj.getY2() - dObj.getY1()) );
	}

	@Override
	public BoundBox getBounds(boolean padded)
	{
		BoundBox box = new BoundBox();
		
		int x1 = dObj.getX1();
		int y1 = dObj.getY1();
		int x2 = dObj.getX2();
		int y2 = dObj.getY2();
		
		box.setX( ( (x1 < x2)? x1 : x2 ) - Utils.MIN_DIST);
		box.setY( ( (y1 < y2)? y1 : y2 ) - Utils.MIN_DIST);
		
		int w = dObj.getX2() - dObj.getX1();
		w = ( (w < 0)? -w : w ) + Utils.MIN_DIST*2 + 1;
		
		int h = dObj.getY2() - dObj.getY1();
		h = ( (h < 0)? -h : h ) + Utils.MIN_DIST*2 + 1;
		
		box.setWidth(w);
		box.setHeight(h);
		
		return box;
	}

	@Override
	public boolean containsPoint(Point p)
	{
		if ( !this.getBounds(true).contains(p) )
			return false;
		
		if (this.distSqToShape(p) <= Utils.MIN_DIST_SQ)
			return true;
		
		return false;
	}

	@Override
	public void DrawControlPoints(Graphics g) 
	{
		this.DrawControlPoint(dObj.getX1(), dObj.getY1(), g);
		this.DrawControlPoint(dObj.getX2(), dObj.getY2(), g);
	}

	@Override
	public int calcControlPoint(Point a) 
	{
		Point b = new Point(dObj.getX1(), dObj.getY1());
		if ( distSq(a, b) <= Utils.MIN_DIST_SQ )
			return 1;
		
		b = new Point(dObj.getX2(), dObj.getY2());
		if ( distSq(a, b) <= Utils.MIN_DIST_SQ )
			return 2;
	
		
		return 0;
	}

}
